package com.lost.octopus.es.processor.config.es.alias;

import com.lost.octopus.es.helper.service.DocumentEsService;
import com.lost.octopus.es.processor.util.ApplicationUtil;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.data.elasticsearch.core.AliasIndexOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;

import javax.annotation.PostConstruct;

/**
 * 重写了{@link ElasticsearchRestTemplate#indexOps(Class)} 返回其他都一样，
 * indexName可以自定义的操作对象
 * @author zhangbh
 * @date 2022/10/31 0031 14:16
 */
public class AliasElasticsearchRestTemplate extends ElasticsearchRestTemplate {

    private DocumentEsService documentEsService;

    public AliasElasticsearchRestTemplate(RestHighLevelClient client) {
        super(client);
    }

    public AliasElasticsearchRestTemplate(RestHighLevelClient client, ElasticsearchConverter elasticsearchConverter) {
        super(client, elasticsearchConverter);
    }

    @PostConstruct
    public void init() {
        // 因为此服务已经实现了一些基础服务，注入的时候可以复用简化部分逻辑
        documentEsService = ApplicationUtil.getBean(DocumentEsService.class);
    }

    @Override
    public IndexOperations indexOps(Class<?> clazz) {
        return new AliasIndexOperations(this, clazz, documentEsService);
    }
}
